//
//  GAOStatusesToolBarView.swift
//  Weibo28
//
//  Created by 高辰 on 16/5/14.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

//微博底部ToolBar
class GAOStatusesToolBarView: UIView {
    //懒加载button
    private lazy var retweetButton: UIButton = self.addChildButton("timeline_icon_retweet", title: "转发")
    
    private lazy var commentButton: UIButton = self.addChildButton("timeline_icon_comment", title: "评论")
    
    private lazy var unlikeButton: UIButton = self.addChildButton("timeline_icon_unlike", title: "赞")
    
    var statusesViewmModel: GAOStatusesViewModel? {
        didSet{
            
            retweetButton.setTitle(statusesViewmModel?.retweetCount, forState: UIControlState.Normal)
            commentButton.setTitle(statusesViewmModel?.commentCont, forState: UIControlState.Normal)
            unlikeButton.setTitle(statusesViewmModel?.attitudesCount, forState: UIControlState.Normal)
        }
    }
    
    //手写代码创建View
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
    
//        backgroundColor = UIColor.redColor()
        //创建并添加子控件
        let firstLineView = addLineView()
        let secondLineView = addLineView()
        
        //设置约束
        retweetButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(commentButton)
        }
        commentButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(retweetButton.snp_trailing)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(unlikeButton)
        }
        unlikeButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.leading.equalTo(commentButton.snp_trailing)
        }
        
        firstLineView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetButton.snp_trailing)
            make.centerY.equalTo(self)
        }
        secondLineView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentButton.snp_trailing)
            make.centerY.equalTo(self)
        }
        
        
    }
    /**
     创建子视图按钮
     
     - parameter imageName: 图片名字
     - parameter title:     按钮上的文字
     */
    private func addChildButton(imageName: String, title: String) -> UIButton {
        let button = UIButton()
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        
//        //去掉高亮效果
//        button.adjustsImageWhenHighlighted = false
        
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        addSubview(button)
        return button
    }
    
    //创建线
    private func addLineView() -> UIView {
    
//        let view = UIImageView(image: UIImage(named: "timeline_card_bottom_line_highlighted") )
        
        let view = UIView()
        view.bounds = CGRect(x: 0, y: 0, width: 1, height: self.bounds.size.height-10)
        view.backgroundColor = UIColor.grayColor()
        addSubview(view)
        return view
    }
    

}
